package org.ccay.vfs;

import java.io.File;

import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.auth.StaticUserAuthenticator;
import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder;
import org.ccay.core.ioc.Ccay;
import org.ccay.core.log.CcayLoggerFactory;
import org.ccay.core.log.ILogger;
import org.ccay.core.request.IApplicationPrincipal;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

/**
 * 模块文件系统配置
 * @author chaowangbang
 *
 */
public class ModuleFileSystemConfig implements InitializingBean{
	
	private static final ILogger logger = CcayLoggerFactory.getLogger(ModuleFileSystemConfig.class); 
	 
	private static final String DEFAULT_ARCHIVE_SCHEME= "file";
	
	private static final String ABSTRACT_ROOT = new File("/").getAbsolutePath().replace("\\", "/");
	
	private static String DEFAULT_ARCHIVEROOT = (ABSTRACT_ROOT.equals("/") ? "/" : "/" + ABSTRACT_ROOT ) + "ccay/archive" ;
	
	private static String DEFAULT_WORKROOT = (ABSTRACT_ROOT.equals("/") ? "/" : "/" + ABSTRACT_ROOT ) + "ccay/wrok" ;
	
	
	/**
	 * 模块名称
	 */
	private String module;
	
	/**
	 * 临时文件系统根路径
	 */
	private String tempRoot;
	
	/**
	 * 工作文件系统根路径
	 */
	private String workRoot;
	
	/**
	 * 归档文件系统类型scheme
	 * 取值 参见apache commons-vfs 支持的scheme
	 */
	private String archiveScheme;
	
	/**
	 * 归档文件系统根路径
	 */
	private String archiveRoot;
	
	/**
	 * 归档文件系统 用户名 
	 */
	private String archUsername;

	/**
	 * 归档文件系统 密码
	 */
	private String archPassword;
	
	/**
	 * 归档文件系统 域 例如 localhost:10021
	 */
	private String archDomain;
	
	/**
	 * 归档文件系统 其他属性配置
	 */
	private FileSystemOptions archFileSystemOptions;
	

	@Override
	public void afterPropertiesSet() throws Exception {
		Assert.notNull(module, "Property 'module' is required");
		if(workRoot == null){
			workRoot = DEFAULT_WORKROOT + "/" + getAppName() + "/" + module;
			logger.info("Using default \""+workRoot+"\" as work files store for "+module+" module.");
		}
		if(tempRoot == null){
			tempRoot =  "/" + getAppName() + "/" + module;
			logger.info("Using default \""+tempRoot+"\" as temporary files store for "+module+" module.");
		}
		
		if(archiveScheme != null && archiveRoot == null){
			Assert.notNull(archiveRoot, "Property 'archiveRoot' is required");
		}
		if(archiveScheme == null){
			archiveScheme = DEFAULT_ARCHIVE_SCHEME;
			logger.info("Using local file as archive file system for "+module+"module.");
		}
		if(archiveRoot == null){
			archiveRoot = DEFAULT_ARCHIVEROOT + "/" + module;
			logger.info("Using default \""+archiveRoot+"\" as archive files store for "+module+" module.");
		}
		if(archFileSystemOptions == null){
			archFileSystemOptions = new FileSystemOptions();
		}
		if(archUsername != null || archPassword != null || archDomain != null){
			StaticUserAuthenticator auth = new StaticUserAuthenticator(archDomain, archUsername, archPassword);
	    	DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(archFileSystemOptions, auth);
		}
	}
	
	private String getAppName(){
		return "ccay";
//		IApplicationPrincipal appBean = Ccay.getContext().getBean("applicationPrincipal", IApplicationPrincipal.class);
//		return appBean.getAppName();
	}

	public String getModule() {
		return module;
	}

	public void setModule(String module) {
		this.module = module;
	}

	public String getWorkRoot() {
		return workRoot;
	}

	public void setWorkRoot(String workRoot) {
		this.workRoot = workRoot;
	}


	public String getArchiveRoot() {
		return archiveRoot;
	}

	public void setArchiveRoot(String archiveRoot) {
		this.archiveRoot = archiveRoot;
	}

	public String getArchUsername() {
		return archUsername;
	}

	public void setArchUsername(String archUsername) {
		this.archUsername = archUsername;
	}

	public String getArchPassword() {
		return archPassword;
	}

	public void setArchPassword(String archPassword) {
		this.archPassword = archPassword;
	}

	public String getArchDomain() {
		return archDomain;
	}

	public void setArchDomain(String archDomain) {
		this.archDomain = archDomain;
	}

	public FileSystemOptions getArchFileSystemOptions() {
		return archFileSystemOptions;
	}

	public void setArchFileSystemOptions(FileSystemOptions archFileSystemOptions) {
		this.archFileSystemOptions = archFileSystemOptions;
	}


	public String getArchiveScheme() {
		return archiveScheme;
	}

	public void setArchiveScheme(String archiveScheme) {
		this.archiveScheme = archiveScheme;
	}

	public String getTempRoot() {
		return tempRoot;
	}
}
